package com.zhisheng.inteceptor;

/*
    全哥
*/

import com.alibaba.fastjson.JSONObject;
import com.zhisheng.users.entity.Users;
import com.zhisheng.utils.MyJwtUtils_0_9_1;
import com.zhisheng.utils.MyThreadLocal;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class MyLoginInteceptor implements HandlerInterceptor {

    @Autowired
    private MyJwtUtils_0_9_1 myJwtUtils_0_9_1;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 只要代码能执行到这里,就说明已经经过网关的校验了,一定能取到值!!!
        String token = request.getHeader("loginUser");
        //4: 将令牌中的信息转为 User对象
        Users users = JSONObject.parseObject(token, Users.class);
        //5: 将User对象存入到ThreadLocal中
        MyThreadLocal.set(users);
        return true;
    }

}
